import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import externalGlobals from 'rollup-plugin-external-globals';
import path from 'path';
import { viteStaticCopy } from 'vite-plugin-static-copy';

export default defineConfig({
    plugins: [
        vue(),
        viteStaticCopy({
            targets: [
                {
                    src: 'node_modules/@glowjs/core/dist/glow.core.js',
                    dest: 'glowjs/js',
                },
                {
                    src: 'node_modules/@glowjs/core/dist/glow.core.js',
                    dest: '../public/glowjs/js',
                }
            ]
        })
    ],
    server: {
        port: 3000,
        open: true
    },
    build: {
        outDir: 'dist',
        assetsDir: '/',
        emptyOutDir: true,
        chunkSizeWarningLimit: 10240,
        commonjsOptions: {
            transformMixedEsModules: true
        },
        rollupOptions: {
            external: [
                '@glowjs/core'
            ],
            plugins: [
                externalGlobals({
                    '@glowjs/core': 'GLOW'
                })
            ],
            output: {
                entryFileNames: `js/client.js`,
                chunkFileNames: `js/[name].js`,
                assetFileNames: chunkInfo => {
                    if (path.extname(chunkInfo.name!) === '.css') {
                        return `css/client.[ext]`;
                    }
                    return `images/[name].[ext]`;
                },
                manualChunks: {}
            }
        }
    }
});